package com.itheima.Client;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class MyRunable implements Runnable{

    private Socket socket;
    public MyRunable(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            //接收服务器返回的数据
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            if (br.read()==1) {
                System.out.println("服务器已经连接成功");
                menu(socket);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    Scanner sc = new Scanner(System.in);

    private void menu(Socket socket) throws IOException {
        while (true) {
            System.out.println("==============欢迎来到我的聊天室================");
            System.out.println("1登录");
            System.out.println("2注册");
            System.out.println("请输入您的选择：");
            //接收用户的输入
            String s = sc.nextLine();
            //将用户的输入发送给服务器
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write(s);
            bw.newLine();
            bw.flush();
            if(s.equals("1")) {
                //登录
                login(socket);
            }else if(s.equals("2")) {
                //注册
                register(socket);
            }else {
                System.out.println("输入有误，请重新输入");
            }
        }
    }

    private void register(Socket socket) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        System.out.println("开始注册");
        System.out.println("请输入用户名：");
        String username = sc.nextLine();
        System.out.println("请输入密码：");
        String password = sc.nextLine();
        //将用户名和密码发送给服务器
        bw.write("username="+username+"&password="+password);
        bw.newLine();
        bw.flush();
        //接收服务器回写数据
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String s = br.readLine();
        System.out.println(s);
        if (s.equals("注册成功")) {
            System.out.println("注册成功,开始登录");
            login(socket);
        }else if (s.equals("用户名重复")) {
            System.out.println("用户名已存在,请重新注册");
        }else if (s.equals("密码格式错误")) {
            System.out.println("密码格式错误,请重新注册");
        }else if (s.equals("用户名格式错误")) {
            System.out.println("用户名格式错误,请重新注册");
        }
    }

    private void login(Socket socket) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        //登录
        System.out.println("请输入用户名：");
        String username = sc.nextLine();
        System.out.println("请输入密码：");
        String password = sc.nextLine();

        //将用户名和密码发送给服务器
        bw.write("username="+username+"&password="+password);
        bw.newLine();
        bw.flush();

        //接收服务器回写数据
        String s = br.readLine();
        System.out.println(s);

        //判断
        if (s.equals("登录成功")) {
            System.out.println("登录成功,开始聊天");
            System.out.println("请输入您要说的话：");
            while (true) {
                //单独开个线程接收服务器回写数据
                new MyThread(socket).start();

                Scanner sc = new Scanner(System.in);
                String talk = sc.nextLine();
                //将说的话发送给服务器
                bw.write(talk);
                bw.newLine();
                bw.flush();
            }
        }
    }
}
